//
//  AppDelegate.swift
//  iOSweibo
//
//  Created by lzm on 16/5/9.
//  Copyright © 2016年 lzm. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
//        let tabBarVC = MtabBarController()
        
        window?.rootViewController = defaultVC()
//        tabBarVC.view.backgroundColor = UIColor.grayColor()
        setupApp()
        
        
        window?.makeKeyAndVisible()
        
        
        return true
    }
    
    //程序启动后默认的控制器
    private func defaultVC() -> UIViewController{
        if !MUserAccountViewModel.sharedInstance.isUserLogin
        {
            return MtabBarController()
        }
        //判断是否新版本
        return isNewVersion() ? MNewFeatureVC() : MWelcomeVCViewController()
    }
    
    //切换控制器
    func switchRootVC(controller : UIViewController){
        window?.rootViewController = controller
    }
    
    
    //判断是否新版本
    func isNewVersion() ->Bool
    {
        //获取当前版本号
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        //获取上一次版本号
        let sandboxVersionKey = "sandboxVersionKey"
        let sandboxVersion = NSUserDefaults.standardUserDefaults().stringForKey(sandboxVersionKey)
        
        //比较版本号  只有不同的版本号才为最新版本号
        let isNewVersion = currentVersion != sandboxVersion
        //如果是最新版本,将最新版本保存
        if isNewVersion{
            NSUserDefaults.standardUserDefaults().setObject(currentVersion, forKey: sandboxVersionKey)
            NSUserDefaults.standardUserDefaults().synchronize()
        }
        return isNewVersion
    }
    
    
    //设置整个UINavigationBar的颜色
    private func setupApp(){
        let bar = UINavigationBar.appearance()
        bar.tintColor = UIColor.orangeColor()
    }

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

